﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace AirplaneReservation
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                for (int i = 18; i < 90; i++)
                {
                    age.Items.Add(i.ToString());
                }

                if (Session["ssRole"].ToString() == "member")
                {
                    SqlConnection ccon = new SqlConnection(ConfigurationManager.ConnectionStrings["airplaneConnectionString"].ToString());
                    ccon.Open();
                    string select = "select UserName, Password, FirstName, LastName, Address, PhoneNumber, Email, Sex, Age, CreditCard, SkyMiles, Passport, CustomerType.Name as t from Member join Customer on Member.Id=Customer.MemberId, CustomerType where CustomerType.Id=Customer.CustomerType and UserName='" + Session["ssUser"] + "' ";
                    SqlDataAdapter da = new SqlDataAdapter(select, ccon);
                    DataTable dt = new DataTable();
                    da.Fill(dt);

                    userName.Text = dt.Rows[0][0].ToString();
                    passW.Text = dt.Rows[0][1].ToString();
                    firstN.Text = dt.Rows[0][2].ToString();
                    lastN.Text = dt.Rows[0][3].ToString();
                    address.Text = dt.Rows[0][4].ToString();
                    phone.Text = dt.Rows[0][5].ToString();
                    email.Text = dt.Rows[0][6].ToString();

                    if (dt.Rows[0][7].ToString() == "True")
                        sex.SelectedItem.Text = "Male";
                    else sex.SelectedItem.Text = "Female";

                    age.Text = dt.Rows[0][8].ToString();

                    drlType.Text = dt.Rows[0]["t"].ToString();

                    credit.Items.Insert(0, dt.Rows[0][9].ToString());
                    Session["ssCredit"] = credit.Text;

                    mile.Text = dt.Rows[0]["SkyMiles"].ToString();
                    passport.Text = dt.Rows[0]["Passport"].ToString();

                    ccon.Close();
                }
            }
        }

        protected void btnLogIn_Click(object sender, EventArgs e)
        {
            SqlConnection ccon = new SqlConnection(ConfigurationManager.ConnectionStrings["airplaneConnectionString"].ToString());
            ccon.Open();

            string select = "select Member.Id as a, UserName, Password, Name from Member join Role on Member.RoleId=Role.Id where UserName='" + txtUser.Text + "' and Password='" + txtPassword.Text + "' ";
            SqlDataAdapter da = new SqlDataAdapter(select, ccon);
            DataTable dt = new DataTable();
            da.Fill(dt);

            int count = dt.Rows.Count;
            if (count == 0)
            {
                lbError.Text = "Wrong user name or password...";
            }
            else
            {
                if (dt.Rows[0]["Name"].ToString() == "Member")
                {
                    Session["ssId"] = dt.Rows[0]["a"].ToString();
                    Session["ssUser"] = dt.Rows[0]["UserName"].ToString();
                    Session["ssRole"] = "member";
                    Response.Redirect("~/Default.aspx");
                }
                else
                    Response.Redirect("~/Register.aspx");
            }

            ccon.Close();
        }

        protected void logOut_Click(object sender, ImageClickEventArgs e)
        {
            Session.Abandon();
            Response.Redirect("~/Default.aspx");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (btnEdit.Text == "Edit")
            {
                btnEdit.Text = "Update";
                enableTextBox();

                credit.DataSourceID = "SqlDataSource1";
                credit.DataValueField = "Code";
                credit.DataTextField = "Code";
                credit.DataBind();
                credit.Items.Insert(0, Session["ssCredit"].ToString());
            }
            else
            {
                btnEdit.Text = "Edit";
                disableTextBox();

                SqlConnection ccon = new SqlConnection(ConfigurationManager.ConnectionStrings["airplaneConnectionString"].ToString());
                ccon.Open();

                SqlCommand cmd = new SqlCommand("UpdateProfile", ccon);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add("@userId", SqlDbType.VarChar).Value = Session["ssId"].ToString();
                cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = passW.Text;
                cmd.Parameters.Add("@credit", SqlDbType.VarChar).Value = credit.Text;
                cmd.Parameters.Add("@address", SqlDbType.NVarChar).Value = address.Text;
                cmd.Parameters.Add("@phone", SqlDbType.VarChar).Value = phone.Text;
                cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = email.Text;
                cmd.Parameters.Add("@age", SqlDbType.Int).Value = age.Text;

                cmd.ExecuteNonQuery();
                cmd.Dispose();

                ccon.Close();
            }
        }

        void enableTextBox()
        {
            passW.ReadOnly = false;
            address.ReadOnly = false;
            phone.ReadOnly = false;
            email.ReadOnly = false;
            age.Enabled = true;
            age.Font.Bold = false;
            credit.Enabled = true;
            credit.Font.Bold = false;
        }

        void disableTextBox()
        {
            passW.ReadOnly = true;
            address.ReadOnly = true;
            phone.ReadOnly = true;
            email.ReadOnly = true;
            age.Enabled = false;
            age.Font.Bold = false;
            credit.Enabled = false;
            credit.Font.Bold = false;
        }
    }
}